using System;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO;

namespace FireStarterModeller.BO
{
    public class DMInheritanceRelationshipDiscriminatorMustBeStringProp : IBusinessObjectRule
    {
        private DMInheritanceRelationship _inheritanceRelationship;

//        public DMInheritanceRelationshipDiscriminatorMustBeStringProp(DMInheritanceRelationship inheritanceRelationship)
//        {
//            if (inheritanceRelationship == null) throw new ArgumentNullException("inheritanceRelationship");
//            _inheritanceRelationship = inheritanceRelationship;
//        }

        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            _inheritanceRelationship = (DMInheritanceRelationship) bo;
            if (_inheritanceRelationship.InheritanceType == ORMapping.ConcreteTableInheritance) return true;
            if (_inheritanceRelationship.InheritanceType == ORMapping.ClassTableInheritance && String.IsNullOrEmpty(_inheritanceRelationship.Discriminator)) return true;
            Criteria propertyNameCriteria = new Criteria("PropertyName", Criteria.ComparisonOp.Equals, _inheritanceRelationship.Discriminator);
            Criteria classIDCriteria = new Criteria("ClassID", Criteria.ComparisonOp.Equals, _inheritanceRelationship.SuperClassID);
            Criteria combinedCriteria = new Criteria(propertyNameCriteria, Criteria.LogicalOp.And, classIDCriteria);
            DMProperty property = BORegistry.DataAccessor.BusinessObjectLoader.GetBusinessObject<DMProperty>(combinedCriteria);
            if (property == null) return true;
            return property.PropertyTypeString == DMPropertyType.PROP_TYPE_STRING || property.PropertyType.AssemblyName != "System";
        }

//
        public string Name
        {
            get { return "DMInheritanceRelationshipDiscriminatorMustBeStringProp"; }
        }

        public string Message
        {
            get { return string.Format("The discriminator for the superclass of '{0}' must be a Property of type 'String' or a CustomType that can be resolved to string", _inheritanceRelationship.SubClass); }
        }

        public ErrorLevel ErrorLevel
        {
            get { return ErrorLevel.Warning; }
        }
    }
}